package com.menu.web.controller.Information;

import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.core.domain.entity.MenuProtype;
import com.menu.common.enums.BusinessType;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.system.service.IMenuInformtypeService;
import com.menu.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 菜单权限Controller
 *
 * @author ruoyi
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/system/informtype")
public class MenuInformtypeController extends BaseController {
    @Autowired
    private IMenuInformtypeService menuInformtypeService;
    @Autowired
    private ISysMenuService menuService;

    /**
     * 查询菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:informtype:list')")
    @GetMapping("/list")
    public AjaxResult list(MenuProtype menuProtype) {
        List<MenuProtype> list = menuInformtypeService.selectMenuInformtypeList(menuProtype);
        return success(list);
    }

    /**
     * 导出菜单权限列表
     */
    @PreAuthorize("@ss.hasPermi('system:informtype:export')")
    @Log(title = "菜单权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuProtype menuProtype) {
        List<MenuProtype> list = menuInformtypeService.selectMenuInformtypeList(menuProtype);
        ExcelUtil<MenuProtype> util = new ExcelUtil<MenuProtype>(MenuProtype.class);
        util.exportExcel(response, list, "菜单权限数据");
    }

    /**
     * 获取菜单权限详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:informtype:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId) {
        return success(menuInformtypeService.selectMenuInformtypeByProductId(productId));
    }

    /**
     * 新增菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:informtype:add')")
    @Log(title = "菜单权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuProtype menuProtype) {
        return toAjax(menuInformtypeService.insertMenuInformtype(menuProtype));
    }

    /**
     * 修改菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:informtype:edit')")
    @Log(title = "菜单权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuProtype menuProtype) {
        return toAjax(menuInformtypeService.updateMenuInformtype(menuProtype));

    }

    /**
     * 删除菜单权限
     */
    @PreAuthorize("@ss.hasPermi('system:informtype:remove')")
    @Log(title = "菜单权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds) {

        for (Long productId : productIds) {
            MenuProtype menuProtype = new MenuProtype();
            menuProtype.setParentId(productId);
            if (!menuInformtypeService.selectMenuInformtypeList(menuProtype).isEmpty()) {
                return AjaxResult.error("存在子节点不允许删除");
            }

        }

        return toAjax(menuInformtypeService.deleteMenuInformtypeByProductIds(productIds));
    }

    @GetMapping("/treeselect")
    public AjaxResult treeselect(MenuProtype menuProtype) {
        List<MenuProtype> menus = menuInformtypeService.selectMenuInformtypeList(menuProtype);
        return success(menuInformtypeService.buildMenuTreeSelect(menus));
    }
}
